package org.jeecg.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.mapper.SysDictItemMapper;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @Author zhangweijian
 * @since 2018-12-28
 */
@Service
public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDictItem> implements ISysDictItemService {

    @Autowired
    private SysDictItemMapper sysDictItemMapper;

    @Override
    public List<SysDictItem> selectItemsByMainId(String mainId) {
        return sysDictItemMapper.selectItemsByMainId(mainId);
    }

    @Override
    public void checkModel(SysDictItem vo){
        if (StringUtils.isBlank(vo.getDictId())){
            throw new JeecgBootException("请选择对应的数据字典！");
        }
        if (StringUtils.isBlank(vo.getItemText())){
            throw new JeecgBootException("请填写数据名称！");
        }
        if (StringUtils.isBlank(vo.getItemValue())){
            throw new JeecgBootException("请填写数据值！");
        }
        LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(vo.getId())) {
            queryWrapper.ne(SysDictItem::getId, vo.getId());
        }
        queryWrapper.eq(SysDictItem::getDictId, vo.getDictId());
        queryWrapper.eq(SysDictItem::getItemValue, vo.getItemValue());
        queryWrapper.last("LIMIT 1");
        SysDictItem dictItem = baseMapper.selectOne(queryWrapper);
        if (dictItem != null){
            throw new JeecgBootException("数据值已存在！");
        }
    }
}
